package org.Iteracion3.presentacion;
import java.awt.EventQueue;

import javax.swing.JFrame;

import java.awt.BorderLayout;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import org.Iteracion3.dominio.Billete;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class AnularBillete {

	public JFrame frmAnularBillete;
	private JPanel pnlAnularBillete;
	private static JList listaBilletes;
	private JLabel lblCliente;
	private JLabel lblIdViaje;
	private JLabel lblAsiento;
	private JLabel lblPrecio;
	private JButton btnAnular;
	private JButton btnAtras;
	private static JLabel lblIdViajeDato;
	private static JLabel lblClienteDato;
	private static JLabel lblAsientoDato;
	private static JLabel lblPrecioDato;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AnularBillete window = new AnularBillete();
					window.frmAnularBillete.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * @throws Exception 
	 */
	public AnularBillete() throws Exception {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 * @throws Exception 
	 */
	
	
	private void initialize() throws Exception {
		frmAnularBillete = new JFrame();
		frmAnularBillete.addWindowListener(new FrameWindowListener());
		frmAnularBillete.setTitle("Anular Billete");
		frmAnularBillete.setResizable(false);
		frmAnularBillete.setBounds(100, 100, 430, 268);
		frmAnularBillete.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmAnularBillete.getContentPane().setLayout(new BorderLayout(0, 0));
		{
			pnlAnularBillete = new JPanel();
			frmAnularBillete.getContentPane().add(pnlAnularBillete, BorderLayout.CENTER);
			pnlAnularBillete.setLayout(null);
			{
				listaBilletes = new JList();
				listaBilletes.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent arg0) {
						try {
							cargarCampos(Integer.parseInt(listaBilletes.getSelectedValue().toString()));
							btnAnular.setEnabled(true);
						} catch (NumberFormatException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
				listaBilletes.setBorder(new TitledBorder(null, "Lista de Billetes", TitledBorder.LEADING, TitledBorder.TOP, null, null));
				listaBilletes.setModel(new AbstractListModel() {
					String[] values = new String[] {};
					public int getSize() {
						return values.length;
					}
					public Object getElementAt(int index) {
						return values[index];
					}
				});
				listaBilletes.setBounds(10, 11, 158, 170);
				pnlAnularBillete.add(listaBilletes);
				actualizarLista();
			}
			{
				lblCliente = new JLabel("Cliente");
				lblCliente.setBounds(192, 70, 46, 14);
				pnlAnularBillete.add(lblCliente);
			}
			{
				lblIdViaje = new JLabel("Id del viaje");
				lblIdViaje.setBounds(192, 35, 71, 14);
				pnlAnularBillete.add(lblIdViaje);
			}
			{
				lblAsiento = new JLabel("Asiento");
				lblAsiento.setBounds(192, 106, 46, 14);
				pnlAnularBillete.add(lblAsiento);
			}
			{
				lblPrecio = new JLabel("Precio");
				lblPrecio.setBounds(192, 143, 46, 14);
				pnlAnularBillete.add(lblPrecio);
			}
			{
				btnAnular = new JButton("Anular");
				btnAnular.setEnabled(false);
				btnAnular.addActionListener(new BtnAnularActionListener());
				btnAnular.setBounds(10, 195, 89, 23);
				pnlAnularBillete.add(btnAnular);
			}
			{
				btnAtras = new JButton("Atras");
				btnAtras.addActionListener(new BtnAtrasActionListener());
				btnAtras.setBounds(315, 195, 89, 23);
				pnlAnularBillete.add(btnAtras);
			}
			{
				lblIdViajeDato = new JLabel("");
				lblIdViajeDato.setFont(new Font("Tahoma", Font.BOLD, 12));
				lblIdViajeDato.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
				lblIdViajeDato.setBounds(273, 35, 89, 14);
				pnlAnularBillete.add(lblIdViajeDato);
			}
			{
				lblClienteDato = new JLabel("");
				lblClienteDato.setFont(new Font("Tahoma", Font.BOLD, 12));
				lblClienteDato.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
				lblClienteDato.setBounds(273, 70, 89, 14);
				pnlAnularBillete.add(lblClienteDato);
			}
			{
				lblAsientoDato = new JLabel("");
				lblAsientoDato.setFont(new Font("Tahoma", Font.BOLD, 12));
				lblAsientoDato.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
				lblAsientoDato.setBounds(273, 106, 89, 14);
				pnlAnularBillete.add(lblAsientoDato);
			}
			{
				lblPrecioDato = new JLabel("");
				lblPrecioDato.setFont(new Font("Tahoma", Font.BOLD, 12));
				lblPrecioDato.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
				lblPrecioDato.setBounds(273, 143, 89, 14);
				pnlAnularBillete.add(lblPrecioDato);
			}
		}
		
	}
	public static void actualizarLista() throws Exception{
		DefaultListModel modelo = new DefaultListModel();
		Billete b=new Billete(0,0, null, null , null); 
		List<Billete> lisBi=b.leerTodos();
		for(int i = 0; i<lisBi.size(); i++){
		        modelo.addElement(lisBi.get(i).getId());
		}
		listaBilletes.setModel(modelo);
	}
	private class BtnAtrasActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			frmAnularBillete.setVisible(false);
		}
	}
	private class BtnAnularActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			Billete b;
			try {
				b = new Billete(Integer.parseInt(listaBilletes.getSelectedValue().toString()), 0, null, null , null);
				if(b.eliminar()==1){
					JOptionPane.showMessageDialog(frmAnularBillete, "Se ha eliminado el billete seleccionado correctamente.","Billete eliminado",JOptionPane.INFORMATION_MESSAGE);
					actualizarLista();
					borrarCampos();
				}else{
					JOptionPane.showMessageDialog(frmAnularBillete, "Error al eliminar billete, int�ntelo de nuevo.","Aviso",JOptionPane.ERROR_MESSAGE);
				}
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static void cargarCampos(int id) throws Exception{
		Billete b= new Billete(id, 0, null, null , null);
		b.leerBillete();
		lblIdViajeDato.setText(Integer.toString(b.getViaje().getId()));
		lblPrecioDato.setText(Double.toString(b.getPrecio()));
		lblClienteDato.setText(b.getCliente().getDni());
		lblAsientoDato.setText(b.getAsiento());
	}
	public static void borrarCampos(){
		lblIdViajeDato.setText("");
		lblPrecioDato.setText("");
		lblClienteDato.setText("");
		lblAsientoDato.setText("");
	}
	private class FrameWindowListener extends WindowAdapter {
		@Override
		public void windowClosing(WindowEvent arg0) {
			frmAnularBillete.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
			if(JOptionPane.showConfirmDialog(frmAnularBillete, "¿Esta seguro que desea salir de la aplicación? \n   Los cambios no guardados se perderán","ATENCION!",JOptionPane.OK_CANCEL_OPTION) == JOptionPane.YES_OPTION){
				System.exit(0);
			}
		}
	}
}
